<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;
use App\Partner;
use App\Manage;
use Illuminate\Support\Facades\Redirect;

class PartnerController extends Controller
{
    //合作商管理
    //合作商列表
    public function index() {
        $role = Session::get('roleid');
        $list = Partner::all();
//        dd($list);
//        if($role ==1 ) {
//            $list = Partner::all();
//        }
//        else {
//            $list = Partner::where('par_id',Session::get('id'))->get();
//        }
        return view('admin.partner.partnerList',['list'=>$list]);
    }

    //获取要修改的id
    public function getPartner($id) {
        $list = Partner::find($id)->toArray();
        $role = Manage::where('roleid',2)->get();
        return view('admin.partner.updatePartner',['list'=>$list,'role'=>$role]);
    }

    //更新信息
    public function updatePartner() {
        $id = input::get('id');
        $data['name'] = input::get('name');
        $data['content'] = input::get('content');
        $data['par_id'] = input::get('man');
        $map = Partner::where('id',$id)->update($data);
        if($map) {
            return Redirect::to('admin/partner')->withErrors('更新成功!');
        }
        else {
            return back()->withErrors('更新失败!');
        }
    }

    //载入添加页
    public function add() {
        $list = Manage::where('roleid',2)->get();
        return view('admin.partner.addPartner',['list'=>$list]);
    }

    //添加信息
    public function addPartner() {
        $name = input::get('name');
        $content = input::get('content');
        $map = Partner::where('name',$name)->first();
        if(!$map) {
            $list = Partner::insert(['name'=>$name,'content'=>$content]);
            if($list) {
                return Redirect::to('admin/partner')->withErrors('添加成功!');
            }
            else {
                return back()->withErrors('添加失败!');
            }
        }
        else {
            return back()->withErrors('该名称已存在!');
        }
    }

    //删除客户商
    public function delPartner($id) {
        if(Session::get('rolied') > 1){
            return back()->withErrors('权限不足!');
        }
        $del = Partner::where('id',$id)->delete();
        if($del) {
            return back()->withErrors('删除成功!');
        }
        else {
            return back()->withErrors('删除失败!');
        }
    }
}
